<template>
  <el-dialog
    title="分配权限"
    :visible="isShow"
    width="700px"
    @close="closeEvent"
  >
    <!-- props:{label:'label',children:'children'} -->
    <el-tree
      ref="tree"
      check-strictly
      node-key="id"
      :default-checked-keys="permIds"
      show-checkbox
      :data="treeData"
      :props="{ label: 'name' }"
    />
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils'
import { sysRoleId, sysRoleAssignPrem } from '@/api/setting'
export default {
  props: {
    isShow: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      treeData: [],
      permIds: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysPermission()
      this.treeData = changeData(res.data, '0')
      console.log('权限列表', res)
    },
    closeEvent() {
      this.$emit('update:isShow', false)
      // 清空选中
      this.$refs.tree.setCheckedKeys([])
    },
    async getRoleInfo(id) {
      this.id = id
      const res = await sysRoleId(id)
      // 用于el-tree默认选中：它只能用于默认选中，而且是在以前基础上的累加
      // this.permIds = res.data.permIds
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log('getRoleInfo', res)
    },
    // 确定点击
    async submit() {
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配权限成功')
      this.closeEvent()
    }
  }
}
</script>
<style></style>
